package com.zzx.test.future;

import com.zzx.test.entity.Dog;
import org.junit.Test;

import java.io.*;
import java.util.Arrays;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;

public class SeventeenClass1 {

    @Test
    public void testStrictfp1() {
        testStrictfp2();
    }

    public strictfp static void testStrictfp2() {
        float aFloat = 0.6666666666666666666f;
        double aDouble = 0.88888888888888888d;
        double sum = aFloat + aDouble;
        System.out.println("sum: " + sum);
    }

    @Test
    public void randomTest1() {
        RandomGeneratorFactory<RandomGenerator> l128X256MixRandom = RandomGeneratorFactory.of("L128X256MixRandom");
        // 使用时间戳作为随机数种子
        RandomGenerator randomGenerator = l128X256MixRandom.create(System.currentTimeMillis());
        for (int i = 0; i < 5; i++) {
            System.out.println(randomGenerator.nextInt(10));
        }
        RandomGeneratorFactory.all().forEach(factory -> {
            System.out.println(factory.group() + ":" + factory.name());
        });
    }

//    此功能为JDK21的新特性，17已不再支持
    @Test
    public void testSwitch() {
        System.out.println(switch1("123"));
    }

    static String switch1(Object o) {
        return switch (o) {
            case Integer i -> String.format("int %d", i);
            case Long l -> String.format("long %d", l);
            case Double d -> String.format("double %f", d);
            case String s -> String.format("String %s", s);
            default -> o.toString();
        };
    }

    @Test
    public void serializableTest() {
        try {
            Dog dog = new Dog("哈士奇", 5);
            // 序列化 - 对象转字节数组
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
                objectOutputStream.writeObject(dog);
            }
            byte[] bytes = byteArrayOutputStream.toByteArray();
            System.out.println(Arrays.toString(bytes));
            // 反序列化 - 字节数组转对象
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Object object = objectInputStream.readObject();
            System.out.println(object.toString());
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}
